using System;
using System.Collections.Generic;
using System.Linq;

namespace DemoClasses;

/// <summary>
/// Generic repository for managing collections of entities
/// </summary>
public class Repository<T> where T : class
{
    private readonly Dictionary<int, T> _items;
    private int _nextId;

    public int Count => _items.Count;
    public bool IsEmpty => _items.Count == 0;

    public Repository()
    {
        _items = new Dictionary<int, T>();
        _nextId = 1;
    }

    public int Add(T item)
    {
        if (item == null)
            throw new ArgumentNullException(nameof(item));

        int id = _nextId++;
        _items[id] = item;
        return id;
    }

    public T? GetById(int id)
    {
        return _items.TryGetValue(id, out var item) ? item : null;
    }

    public List<T> GetAll()
    {
        return _items.Values.ToList();
    }

    public bool Remove(int id)
    {
        return _items.Remove(id);
    }

    public void Clear()
    {
        _items.Clear();
        _nextId = 1;
    }

    public bool Contains(int id)
    {
        return _items.ContainsKey(id);
    }
}
